import re
import json


def parse_route_table(route_output):
    # 分割输入为行
    if isinstance(route_output, str):
        lines = route_output.strip().split("\n")
    elif isinstance(route_output, list):
        lines = route_output
    else:
        return []

    # 找到表头行和数据起始行
    header_line = next(
        (i for i, line in enumerate(lines) if "Destination/Mask" in line), None
    )
    if header_line is None:
        return []

    # 提取表头
    headers = re.split(r"\s{2,}", lines[header_line].strip())

    # 提取数据行（跳过表头和分隔线）
    data_lines = lines[header_line + 1 :]
    networks = []

    # 正则表达式模式，用于分割每行数据
    pattern = re.compile(
        r"^\s*([\d./]+)\s+(\w+)\s+(\d+)\s+(\d+)\s+(\w+)\s+([\d.]+)\s+(\w+)$"
    )

    for line in data_lines:
        line = line.strip()
        if not line:
            continue

        # 使用正则表达式匹配数据行
        match = pattern.match(line)
        if match:
            dest_mask, proto, pre, cost, flags, nexthop, interface = match.groups()

            # 只处理Direct类型的路由，这些通常代表设备直接连接的网络
            if proto == "Direct":
                # 提取网络地址和掩码
                network, mask = dest_mask.split("/")

                # 提取VLAN ID（如果存在）
                vlan_id = None
                if "Vlanif" in interface:
                    vlan_id = interface.replace("Vlanif", "")

                networks.append(
                    {
                        "network": network,
                        "mask": int(mask),
                        "interface": interface,
                        "vlan_id": vlan_id,
                        "nexthop": nexthop,
                    }
                )

    return networks


if __name__ == "__main__":
    data = """
        Route Flags: R - relay, D - download to fib
        ------------------------------------------------------------------------------
        Routing Tables: Public
        Destinations : 31       Routes : 31
        Destination/Mask    Proto   Pre  Cost      Flags NextHop         Interface
        127.0.0.0/8   Direct  0    0           D   127.0.0.1       InLoopBack0
        127.0.0.1/32  Direct  0    0           D   127.0.0.1       InLoopBack0
        192.168.1.0/24  Direct  0    0           D   192.168.1.253   Vlanif10
        192.168.1.1/32  Direct  0    0           D   127.0.0.1       Vlanif10
        192.168.1.253/32  Direct  0    0           D   127.0.0.1       Vlanif10
        192.168.2.0/24  Direct  0    0           D   192.168.2.253   Vlanif20
        192.168.2.1/32  Direct  0    0           D   127.0.0.1       Vlanif20
        192.168.2.253/32  Direct  0    0           D   127.0.0.1       Vlanif20
        192.168.3.0/24  Direct  0    0           D   192.168.3.253   Vlanif30
        192.168.3.1/32  Direct  0    0           D   127.0.0.1       Vlanif30
        192.168.3.253/32  Direct  0    0           D   127.0.0.1       Vlanif30
        192.168.4.0/24  Direct  0    0           D   192.168.4.253   Vlanif40
        192.168.4.1/32  Direct  0    0           D   127.0.0.1       Vlanif40
        192.168.4.253/32  Direct  0    0           D   127.0.0.1       Vlanif40
        192.168.7.0/24  Direct  0    0           D   192.168.7.253   Vlanif70
        192.168.7.1/32  Direct  0    0           D   127.0.0.1       Vlanif70
        192.168.7.253/32  Direct  0    0           D   127.0.0.1       Vlanif70
        192.168.8.0/24  Direct  0    0           D   192.168.8.253   Vlanif80
        192.168.8.1/32  Direct  0    0           D   127.0.0.1       Vlanif80
        192.168.8.253/32  Direct  0    0           D   127.0.0.1       Vlanif80
        192.168.9.0/24  Direct  0    0           D   192.168.9.253   Vlanif90
        192.168.9.1/32  Direct  0    0           D   127.0.0.1       Vlanif90
        192.168.9.253/32  Direct  0    0           D   127.0.0.1       Vlanif90
        192.168.10.0/24  Direct  0    0           D   192.168.10.253  Vlanif100
        192.168.10.253/32  Direct  0    0           D   127.0.0.1       Vlanif100
        192.168.11.0/24  Direct  0    0           D   192.168.11.253  Vlanif110
        192.168.11.1/32  Direct  0    0           D   127.0.0.1       Vlanif110
        192.168.11.253/32  Direct  0    0           D   127.0.0.1       Vlanif110
        192.168.16.0/24  Direct  0    0           D   192.168.16.10   Vlanif1
        192.168.16.10/32  Direct  0    0           D   127.0.0.1       Vlanif1
        <Hexin1>
        """
    print(json.dumps(parse_route_table(data)))
